﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Light;
using Microsoft.Xna.Framework;

namespace Etapa1
{


    public class UnitLightInterpolator : IScreenUpdateable
    {
        class lightInterpolation
        {
            public DeferredLight dl;
            public Vec3Interpolator vi;
            public Vector3 v1;
            public Vector3 v2;
            public double duration;

            public void SwapColors()
            {
                Vector3 v = v1;
                v1 = v2;
                v2 = v;
            }
                 
        }

        List<lightInterpolation> lights = new List<lightInterpolation>();
        bool onLoopReverse = false;

        public bool OnLoopReverse
        {
            get { return onLoopReverse; }
            set { onLoopReverse = value; }
        }

        public void AddLight(DeferredLight dl, Color c1, Color c2, double duration)
        {
            lightInterpolation l = new lightInterpolation();
            l.dl = dl;
            l.duration = duration;
            l.v1 = c1.ToVector3();
            l.v2 = c2.ToVector3();
            lights.Add(l);
            l.vi = new Vec3Interpolator();
            l.vi.Start(l.v1, l.v2, duration, true);
        }

        public UnitLightInterpolator(SceneScreen screen,bool OnLoopReverse)
            : base(screen)
        {
            this.onLoopReverse = OnLoopReverse;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {            
            foreach (var item in lights)
            {
                item.vi.Update(gameTime);
                item.dl.Color= new Color(item.vi.CurrentValue);

                if (item.vi.IsActive == false && onLoopReverse == true)
                {
                    item.SwapColors();
                    item.vi.Start(item.v1, item.v2, item.duration, true);
                }
            }
            
        }
    }
}
